home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
histo3.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
3KB
|
86 lines
! Proposed solution to exercise 4.1 (Robust histogram program);
begin
integer array count(5 : 200);
integer weight_group, least_weight_group, greatest_weight_group,
number_of_weights, max_weights_in_group, weights_per_asterisk,
asterisk_number, number_of_asterisks;
real weight;
! Read the measurements one by one: ;
outtext(" Please type the weights. "); outimage;
outtext(" Remember to type -1 after the last one!"); outimage;
number_of_weights := 0;
weight := inreal;
while weight ge 0 do
begin
if weight < 5 then
begin
outtext("The weight "); outfix(weight, 2, 0);
outtext(" is too small. Not accepted."); outimage;
end else
if weight > 200 then
begin
outtext("The weight "); outfix(weight, 2, 0);
outtext(" is too great. Not accepted."); outimage;
end
else begin
weight_group := entier(weight);
count(weight_group) := count(weight_group) + 1;
number_of_weights := number_of_weights + 1;
end;
weight := inreal;
end of weight ge 0;
if number_of_weights = 0 then
begin
outtext("You did not type any acceptable weights."); outimage;
outtext(" No histogram is produced."); outimage;
end
else begin
! Find the least and greatest weight_groups containing weights: ;
least_weight_group := 5;
while count(least_weight_group) = 0
do least_weight_group := least_weight_group + 1;
greatest_weight_group := 200;
while count(greatest_weight_group) = 0
do greatest_weight_group := greatest_weight_group - 1;
! Find the maximum number of observations in a group: ;
max_weights_in_group := count(least_weight_group);
for weight_group := least_weight_group + 1 step 1 until greatest_weight_group
do max_weights_in_group := max(max_weights_in_group, count(weight_group));
! Compute the number of weights per asterisk in the histogram:
! (Assumes that no line in the histogram may contain more than 50 asterisks);
weights_per_asterisk := 1 + max_weights_in_group//50;
! Produce the histogram: ;
outtext("You typed "); outint(number_of_weights, 0);
outtext(" acceptable weights."); outimage;
outtext("The histogram:"); outimage; outimage;
outtext("Weight : Number of children"); outimage;
for weight_group := least_weight_group step 1 until greatest_weight_group do
begin
outint(weight_group, 6); outtext(" : ");
number_of_asterisks := count(weight_group)/weights_per_asterisk;
for asterisk_number := 1 step 1 until number_of_asterisks
do outchar('*');
setpos(6 + 3 + max_weights_in_group + 3);
outtext("("); outint(count(weight_group), 0); outtext(")");
outimage;
end of weight_group-repetition;
outimage;
if weights_per_asterisk > 1 then
begin
outint(weights_per_asterisk, 0);
outtext(" weights for each *"); outimage;
outimage;
end;
end of not number_of_weights = 0;
end;